package 多线程初阶.示例测试;

import java.util.Scanner;

/**
 * synchronized保证内存可见性
 */
public class TestDemo16 {
    static class Counter{
        public int flag=0;
    }
    public static void main(String[] args) {
        Counter counter=new Counter();
        Thread threadA=new Thread(()->{
            while(true){
                synchronized (counter){
                    if(counter.flag!=0){
                        break;
                    }
                }
            }
            System.out.println("threadA will die");
        });
        Thread threadB=new Thread(()->{
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入一个数=>");
            counter.flag=scanner.nextInt();
        });
        threadA.start();
        threadB.start();
    }
}
